/**
 * Copyright (c) 2009 Jim Hurne
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package org.tzconverter.tzgroup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import org.tzconverter.AppPrefs;
import org.tzconverter.factory.TimeZoneFactory;
import org.tzconverter.tzdisplay.TimeZoneDisplayFactory;
import org.tzconverter.tzdisplay.TimeZoneDisplayView;

/**
 * Default implementation of the AddTimeZoneModel interface.
 * 
 * 
 * @since May 15, 2009
 */

public class DefaultTimeZoneGroupModel implements TimeZoneGroupModel
{
  private final TimeZoneFactory tzFactory;
  private final TimeZoneDisplayFactory displayFactory;
  private final AppPrefs prefs;
  
  private final List<TimeZonePair> timeZonePairs;
  
  private final Set<String> validTimeZoneIds;
  
  /**
   * @param tzFactory
   * @param displayFactory
   */
  public DefaultTimeZoneGroupModel(TimeZoneFactory tzFactory,
      TimeZoneDisplayFactory displayFactory,
      AppPrefs prefs)
  {
    this.tzFactory = tzFactory;
    this.displayFactory = displayFactory;
    this.prefs = prefs;
    
    this.timeZonePairs = new ArrayList<TimeZonePair>();
    this.validTimeZoneIds = createValidTimeZoneIds();
  }
  
  /**
   * Creates a Set containing all of the valid timeZoneIds.
   * 
   * @return
   */
  private Set<String> createValidTimeZoneIds() {
    String[] ids = TimeZone.getAvailableIDs();
    
    //Using a HashSet because it provides constant-time performance
    //for the contains method.
    return new HashSet<String>(Arrays.asList(ids));
  }

  /* (non-Javadoc)
   * @see org.tzconverter.addtz.AddTimeZoneModel#createTimeZoneDisplay(java.lang.String)
   */
  @Override
  public TimeZoneDisplayView createTimeZoneDisplay(String timeZoneId)
  {   
    TimeZone tz = tzFactory.getTimeZone(timeZoneId);
    TimeZoneDisplayView tzDisplayView = displayFactory.createTimeZoneDisplay(tz);
    
    timeZonePairs.add(new TimeZonePair(timeZoneId, tzDisplayView));
    
    return tzDisplayView;
  }
  
  @Override
  public void removeTimeZoneDisplay(TimeZoneDisplayView remove)
  {
    TimeZonePair toRemove = new TimeZonePair(null, remove);
    timeZonePairs.remove(toRemove);
  }

  /* (non-Javadoc)
   * @see org.tzconverter.addtz.AddTimeZoneModel#isValidTimeZoneId(java.lang.String)
   */
  @Override
  public boolean isValidTimeZoneId(String timeZoneId)
  {
    return validTimeZoneIds.contains(timeZoneId);
  }

  @Override
  public List<String> getSavedTimeZoneIds()
  {
    return prefs.getTimeZoneIds();
  }
  
  @Override
  public List<String> getCurrentTimeZoneIds()
  {
    List<String> timeZoneIds = new ArrayList<String>();
    for(TimeZonePair pair : timeZonePairs)
    {
      timeZoneIds.add(pair.timeZoneId);
    }
    return timeZoneIds;
  }

  @Override
  public void saveCurrentTimeZoneIds()
  {
    prefs.setTimeZoneIds(getCurrentTimeZoneIds());
  }
  
  /**
   * Represnets the pairing of a TimeZoneDisplayView and a timeZoneId.
   * This is so that we can remove timeZoneIds from the model based on the
   * given TimeZoneDisplay.
   * 
   */
  private static class TimeZonePair
  {
    private final String timeZoneId;
    private final TimeZoneDisplayView tzDisplayView;
    
    /**
     * @param timeZoneId
     * @param tzDisplayView
     */
    public TimeZonePair(String timeZoneId, TimeZoneDisplayView tzDisplayView)
    {
      this.timeZoneId = timeZoneId;
      this.tzDisplayView = tzDisplayView;
    }

    @Override
    public boolean equals(Object obj)
    {
      if(obj instanceof TimeZonePair)
      {
        TimeZonePair pair = (TimeZonePair) obj;
        return pair.tzDisplayView == this.tzDisplayView;
      }
      return false;
    }
  }
}
